
var Galaxytool_install_step = 1;
var nextButtonHandler = null;
var PW_mismatch_tooltip = null;

function update_tool() {
	// collect newly introduced variables
	// 5.0: ogame url
	// attributes.php
	var url = dijit.byId("url").get("value");
	var old_version = dijit.byId("old_galaxytool").get("value");
	
	// create config.php and attributes.php
	var xhrArgs = {
		url : "secret/ajax/ajax_update.php?step=3",
		handleAs : "json",
		content : {
			old_version : old_version,
			url : url
		},
		load : function(jsonData) {
			if (jsonData.messages != null) {
				showMessages(jsonData.messages, "./images", false);
			}

			if (jsonData.result == true) {
				dojo.byId("step3update").innerHTML = TEXT_UPDATE_DONE;
			} else {
				dojo.byId("step3update").innerHTML = TEXT_UPDATE_FAILED;
			}
			
			if (jsonData.result == true) {
				dijit.byId("nextButton").set("label", TEXT_INSTALL_CONTINUE);
				dojo.disconnect(nextButtonHandler);
				nextButtonHandler = dojo.connect(dijit.byId("nextButton"), "onClick", next);
			}

		},
		error : function(error) {
			console.log("Error:");
			console.log(error);
		}
	}
	dojo.xhrPost(xhrArgs);

}

function download_data() {
	// download OGame API data
	if (dijit.byId("url").get("value") == "") return;
	
	var xhrArgs = {
		url : "secret/ajax/ajax_update.php?step=4",
		handleAs : "json",
		load : function(jsonData) {
			if (jsonData.messages != null) {
				showMessages(jsonData.messages, "./images", false);
			}

			if (jsonData.result == true) {
				dojo.byId("step4download").innerHTML = TEXT_UPDATE_DONE;
			} else {
				dojo.byId("step4download").innerHTML = TEXT_UPDATE_FAILED;
			}

		},
		error : function(error) {
			console.log("Error:");
			console.log(error);
		}
	}
	dojo.xhrPost(xhrArgs);
}

function validate_step2() {
	dojo.byId("messageArea").style.display = "none";
	var url = dijit.byId("url").get("value");
	
	if (dijit.byId("url").isValid() == false) {
		dijit.byId("url").set("state", "Error");
		return;
	}
	var old_version = dijit.byId("old_galaxytool").get("value");
	
	// validate step2 via ajax request and update step2 html information
	var xhrArgs = {
		url : "secret/ajax/ajax_update.php?step=2",
		content : {
			old_version : old_version,
			url : url
		},		
		handleAs : "json",
		load : function(jsonData) {
			var failed = false;
			if (jsonData.messages != null) {
				showMessages(jsonData.messages, "./images");
				failed = true;
			}

			// set button to "validate" or "continue"
			if (failed == false) {
				dijit.byId("nextButton").set("label", TEXT_INSTALL_CONTINUE);
				dojo.disconnect(nextButtonHandler);
				nextButtonHandler = dojo.connect(dijit.byId("nextButton"), "onClick", next);
			} else {
				dijit.byId("nextButton").set("label", TEXT_INSTALL_VALIDATE);
				dojo.disconnect(nextButtonHandler);
				nextButtonHandler = dojo.connect(dijit.byId("nextButton"), "onClick", validate);
			}

		},
		error : function(error) {
			console.log("Error:");
			console.log(error);
		}
	}
	dojo.xhrPost(xhrArgs);

}


function show_step(old_step, new_step, next_is_validate) {
	// change header of "CenterContentPane"
	dojo.byId("update_progress_title").innerHTML = TEXT_UPDATE_STEPS[new_step];
	// change step selection in progress window
	dojo.byId("progress_step" + old_step).className = "step_disabled";
	dojo.byId("progress_step" + new_step).className = "step_enabled";

	if (next_is_validate == true) {
		dijit.byId("nextButton").set("label", TEXT_INSTALL_VALIDATE);
		dojo.disconnect(nextButtonHandler);
		nextButtonHandler = dojo.connect(dijit.byId("nextButton"), "onClick", validate);
	} else {
		dijit.byId("nextButton").set("label", TEXT_INSTALL_CONTINUE);
		dojo.disconnect(nextButtonHandler);
		nextButtonHandler = dojo.connect(dijit.byId("nextButton"), "onClick", next);
	}

	Galaxytool_install_step = new_step;

	// trigger immediate check on selection for the new step
	switch (new_step) {
	case 3:
		update_tool();
		break;
	case 4:
		dojo.disconnect(nextButtonHandler);
		dojo.byId("nextButton").style.display = "none";
		download_data();
		break;
	}
}

function validate() {
	step = parseInt(Galaxytool_install_step);
	if (step < 2 || step > 8)
		return;

	switch (step) {
	case 2:
		validate_step2();
		break;
	}
}

// validate current step and navigate to next step
function next() {
	dijit.byId("nextButton").set("disabled", true);
	dojo.byId("messageArea").style.display = "none";
	step = parseInt(Galaxytool_install_step);
	if (step < 1 || step > 7)
		return;

	switch (step) {
	case 1:
		show_step(1, 2, true);
		break;
	case 2:
		show_step(2, 3, false);
		break;
	case 3:
		show_step(3, 4, false);
		break;
	default:
		console.log("To be implemented for step " + step);
		return;
	}

	dojo.fx.wipeOut({
		node : dojo.byId("step" + step)
	}).play();
	dojo.fx.wipeIn({
		node : dojo.byId("step" + (step + 1))
	}).play();
	dijit.byId("nextButton").set("disabled", false);
}

function galaxytool_update_init() {
	dojo.disconnect(nextButtonHandler);
	nextButtonHandler = dojo.connect(dijit.byId("nextButton"), "onClick", next);	
}

dojo.ready(galaxytool_update_init);